<template>
	<view class="tabbar">
		<view class="tab" v-for="(item,index) in tabBarList" :key="index" @tap="navigatorTo(item.pagePath)">
			<image class="tab-img" :src="currentPage == item.pagePath?item.selectedIconPath:item.iconPath" mode=""></image>
			<view class="text">{{item.text}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "tabBar",
		props:{
			currentPage:String
		},
		data() {
			return {
				tabBarList: [
					{
						"text": "首页",
						"pagePath": "index",
						"iconPath": "/static/tabbar/index.png",
						"selectedIconPath": "/static/tabbar/indexSelected.png"
					},
					{
						"text": "分类",
						"pagePath": "list",
						"iconPath": "/static/tabbar/list.png",
						"selectedIconPath": "/static/tabbar/listSelected.png"
					}, {
						"text": "购物车",
						"pagePath": "shopcart",
						"iconPath": "/static/tabbar/shop.png",
						"selectedIconPath": "/static/tabbar/shopSelected.png"
					},
					{
						"text": "我的",
						"pagePath": "my",
						"iconPath": "/static/tabbar/my.png",
						"selectedIconPath": "/static/tabbar/mySelected.png"
					}
				]
			};
		},
		onLoad() {
			// #ifdef APP-PLUS || H5
			uni.preloadPage({url:`/pages/index/index`})
			uni.preloadPage({url:`/pages/list/list`})
			uni.preloadPage({url:`/pages/shopcart/shopcart`})
			uni.preloadPage({url:`/pages/my/my`})
			// #endif
		},
		methods:{
			navigatorTo(pageName){
				//重复点击
				if(pageName === this.currentPage) return
				//跳转
				if(pageName === 'shopcart' || pageName === 'my'){
					//是否为登录状态
					if(this.$store.state.user.loginStatus){
						uni.redirectTo({
							url:`/pages/${pageName}/${pageName}`
						})
					}else{
						uni.showToast({
							title:"请先登录",
							icon:"none"
						})
						uni.navigateTo({
							url:`/pages/login/login`
						})
					}
				}else{
					uni.redirectTo({
						url:`/pages/${pageName}/${pageName}`
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabbar{
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		box-sizing: border-box;
		z-index: 999;
		display: flex;
		justify-content: space-around;
		align-items: center;
		background-color: #fff;
		border-top: 2rpx solid #ccc;
		.tab{
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			.tab-img{
				width: 40rpx;
				height: 40rpx;
			}
			.text{
				font-size: 24rpx;
				padding: 10rpx 0;
			}
		}
	}
</style>
